﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using TeacherHelp.Util;

namespace TeacherHelp.BLL
{
    public class ImportSubjectManager
    {

        public string ImportDataTable(DataTable table)
        {
            string erroInfo = string.Empty;
            Log.Info("进行导入题库操作");

            //列名校验
            erroInfo += ColumsCheck(table);
            if (!string.IsNullOrEmpty(erroInfo))
            {
                Log.Info(erroInfo);
                return erroInfo;
            }

            List<Model.Subjects> subjectList = new BLL.SubjectsManager().GetSubejctAll();
            List<Model.Exercises> exerciseList = new List<Model.Exercises>();

            int i = 1;
            //取出数据  数据校验
            foreach (DataRow row in table.Rows)
            {
                Model.Exercises exercises = new Model.Exercises();

                #region 答案数据处理
                Log.Info("答案数据处理中..");
                Regex regex = new Regex("[a-d|A-D]{1}");
                var answerlist = regex.Matches(exercises.Answer);
                List<string> newAnswerlist = new List<string>();
                foreach (var item in answerlist)
                {
                    newAnswerlist.Add(item.ToString().ToUpper());
                }
                newAnswerlist = newAnswerlist.Distinct().ToList();
                exercises.Answer = string.Join(",", newAnswerlist);
                #endregion

                #region 科目名称处理
                Log.Info("科目名称处理中..");
                var subjectName = row["科目名称(需使用系统中已存在的科目)"].ToString();
                if (subjectList.Any(m => m.Name == subjectName))
                {
                    exercises.SubjectsId = subjectList.Single(m => m.Name == subjectName).Id;
                }
                else
                {
                    erroInfo += subjectName + "  科目不存在\n";
                }
                #endregion

                #region 题目难度数据处理
                Log.Info("题目难度处理中..");
                switch (row["题目难度"].ToString())
                {
                    case "简单": exercises.Difficulty = 1; break;
                    case "中等": exercises.Difficulty = 2; break;
                    case "困难": exercises.Difficulty = 3; break;
                    default: erroInfo += row["题目难度"].ToString() + "  数据有误"; break;
                }
                #endregion

                exercises.Content = row["题目内容"].ToString();
                exercises.Answer = row["题目答案(英文逗号分隔)"].ToString();
                exercises.Aoption = row["A选项"].ToString();
                exercises.Boption = row["B选项"].ToString();
                exercises.Coption = row["C选项"].ToString();
                exercises.Doption = row["D选项"].ToString();
                exerciseList.Add(exercises);

                #region 空值校验
                Log.Info("空值校验中..");
                if (exercises.Content.Trim().Length == 0)
                    erroInfo += $"第{i}的题目内容不允许为空\n";

                if (exercises.Answer.Trim().Length == 0)
                    erroInfo += $"第{i}的题目答案(英文逗号分隔)不允许为空\n";

                if (exercises.Difficulty.ToString().Trim().Length == 0)
                    erroInfo += $"第{i}的题目难度不允许为空\n";

                if (exercises.SubjectsId.ToString().Trim().Length == 0)
                    erroInfo += $"第{i}的科目名称(需使用系统中已存在的科目)不允许为空\n";

                if (exercises.Aoption.Trim().Length == 0)
                    erroInfo += $"第{i}的A选项不允许为空\n";

                if (exercises.Boption.Trim().Length == 0)
                    erroInfo += $"第{i}的B选项不允许为空\n";
                #endregion
                i++;
            }

            if (string.IsNullOrEmpty(erroInfo))
            {
                StringBuilder sb = new StringBuilder(200);
                //生成SQL
                exerciseList.ForEach(m => sb.AppendLine(SQLBuilder.GetInsertSQLByModel(m)));
                try
                {
                    int result = DBHelper.ExecuteTransaction(sb.ToString());
                    if (result == -1) erroInfo += "写入数据库失败！";
                    else Log.Info("导入题库SQL写入成功！");
                }
                catch (Exception ex)
                {
                    Log.Error("导入题目进数据库时出现异常！", ex);
                    erroInfo += ex.Message;
                }
            }
            else
            {
                Log.Info("导入题库时 erroInfo=" + erroInfo);
            }
            return erroInfo;

        }

        private string ColumsCheck(DataTable table)
        {
            string erroInfo = string.Empty;
            Log.Info("列名校验中..");
            #region 列名校验
            if (!table.Columns.Contains("题目内容"))
                erroInfo += "题目内容列不存在\n";
            if (!table.Columns.Contains("题目答案(英文逗号分隔)"))
                erroInfo += "题目答案(英文逗号分隔)列不存在\n";
            if (!table.Columns.Contains("题目难度"))
                erroInfo += "题目难度列不存在\n";
            if (!table.Columns.Contains("科目名称(需使用系统中已存在的科目)"))
                erroInfo += "科目名称(需使用系统中已存在的科目)列不存在\n";
            if (!table.Columns.Contains("A选项"))
                erroInfo += "A选项列不存在\n";
            if (!table.Columns.Contains("B选项"))
                erroInfo += "B选项列不存在\n";
            if (!table.Columns.Contains("C选项"))
                erroInfo += "C选项列不存在\n";
            if (!table.Columns.Contains("D选项"))
                erroInfo += "D选项列不存在\n";
            #endregion
            return erroInfo;
        }
    }
}
